package ua.lviv.franko.visualisation;

import java.util.List;

import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

import ua.lviv.franko.mlsFunctions.oneDimension.BaseFunction;
import ua.lviv.franko.mlsFunctions.oneDimension.Particle;
import ua.lviv.franko.mlsFunctions.oneDimension.Values;

public class EFGResult1D {
	protected List<BaseFunction>	baseFunctions;
	protected List<Particle>		particles;
	protected double[]				res;

	public EFGResult1D(double[] res, List<BaseFunction> baseFunctions) {
		this.baseFunctions = baseFunctions;
		this.res = res;
	}

	public EFGResult1D(double[] res, List<BaseFunction> baseFunctions, List<Particle> particles) {
		this.baseFunctions = baseFunctions;
		this.res = res;
		this.particles = particles;
	}

	public void display(double a, double b) {
		XYSeries s1 = new XYSeries("result1", true, true);
		XYSeries s2 = new XYSeries("result2", true, true);
		XYSeries s3 = new XYSeries("result3", true, true);
		int N = 100;
		double h = (b - a) / N;
		for (int j = 0; j <= N; j++) {
			double x = a + h * j;
			double[] tmp = f(x);
			s1.add(x, tmp[0]);
			s2.add(x, tmp[1]);
			s3.add(x, tmp[2]);
		}

		XYSeriesCollection col = new XYSeriesCollection();
		col.addSeries(s1);
		col.addSeries(s2);
		col.addSeries(s3);
		NewOkCancelDialog d = new NewOkCancelDialog(null, false, col);
		d.setLocationRelativeTo(null);
		d.setVisible(true);
	}

	public double[] f(double x) {
		if (particles != null) {
			double[] tmp = new double[3];
			for (int i = 0; i < baseFunctions.size(); i++) {
				BaseFunction bf = baseFunctions.get(i);
				Particle p = bf.getXi();
				if (p.lengthTo(new Particle(x, 0.0)) < p.getRho()) {
					Values[] v = bf.calculateAllFunctions(x);
					for (int j = 0; j < v.length; j++) {
						tmp[0] += v[j].val * res[3 * particles.indexOf(bf.getNodeList().get(j))];
						tmp[1] += v[j].val * res[3 * particles.indexOf(bf.getNodeList().get(j)) + 1];
						tmp[2] += v[j].val * res[3 * particles.indexOf(bf.getNodeList().get(j)) + 2];
					}
					// System.out.println(x + "\t" + tmp[0]);
					return tmp;
				}
			}

			return new double[] { 0.0, 0.0, 0.0 };
		} else {
			double[] tmp = new double[3];
			for (int i = 0; i < baseFunctions.size(); i++) {
				BaseFunction bFunction = baseFunctions.get(i);
				// tmp+=bFunction.calculateDerivatyY(x, y)*res[i];
				double val = bFunction.calculate(x);
				tmp[0] += val * res[3 * i];
				tmp[0] += val * res[3 * i + 1];
				tmp[0] += val * res[3 * i + 2];
			}
			// System.out.println(x + "\t" + tmp[0]);
			return tmp;// - (-5*(x*x) + 5*x);
		}
	}

}
